package com.viamo.concilia.config;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;







import com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl;
import com.sun.org.apache.xml.internal.serialize.*;
import com.viamo.concilia.negocio.Parametro;
import com.viamo.concilia.utils.Crypton;
import com.viamo.concilia.utils.ViamoNetDateUtils;
import com.viamo.concilia.utils.ViamoNetFileUtils;
import com.viamo.concilia.utils.ViamoNetXMLUtils;

public class GlobalParameters {

	 
 public static String getParameterValue(String paramType, String ParameterName){
         
         File xmlFile=new File(ViamoNetGlobalConstants.CONFIGURATION_FILE_FULL_NAME);
         
         return ViamoNetXMLUtils.getValueFromFile(xmlFile, "PARAMETER", paramType, ParameterName);
         
 }
 
 public static List<Parametro> getParameterList(){
         
         List<Parametro> parametros=new ArrayList<Parametro>();
         Hashtable<NamedNodeMap, String> attributes=new Hashtable<NamedNodeMap, String>();
 
         System.out.println(ViamoNetGlobalConstants.CONFIGURATION_FILE_FULL_NAME);
         
         File xmlFile=new File(ViamoNetGlobalConstants.CONFIGURATION_FILE_FULL_NAME);
         attributes=ViamoNetXMLUtils.getTagListFromFile(xmlFile, "PARAMETER");
         
         Enumeration<NamedNodeMap> e = attributes.keys();
         NamedNodeMap obj;
         while (e.hasMoreElements()) {
             Parametro param=new Parametro();
             obj = (NamedNodeMap)e.nextElement();
             param.setParamType(obj.getNamedItem("TYPE").getNodeValue());
                 param.setParamName(obj.getNamedItem("NAME").getNodeValue());
                 String crypted=obj.getNamedItem("CRYPTED").getNodeValue();
                 if (crypted.equalsIgnoreCase("YES")){
                         param.setParamValue(Crypton.decrypt(obj.getNamedItem("VALUE").getNodeValue()));
                         param.setParamCrypted(true);
                 }else{
                         param.setParamValue(obj.getNamedItem("VALUE").getNodeValue());
                         param.setParamCrypted(false);
                 }
                 param.setParamDescription(attributes.get(obj));
                 parametros.add(param);
   }
         
         return parametros;
 }
 
 
 
 public static void updateParameterFile(List <Parametro>parametros){
         
         try{
                 if (backupConfigFile()) {
                 
                         File file = new File(ViamoNetGlobalConstants.CONFIGURATION_FILE_FULL_NAME);
                         file.createNewFile();
                         BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "ISO-8859-1"));
                         out.write(getParamXMLStream(parametros));
                         out.close();
                 }
         }catch (Exception e)
         {
                 
         }
 }
 
 private static String getParamXMLStream(List <Parametro>parametros){
         Document xmlDoc = null;
         
         String xmlParametersStream = "";
         
         try {
                 DocumentBuilderFactory dbFactory = DocumentBuilderFactoryImpl.newInstance();
                 DocumentBuilder docBuilder;
                 docBuilder = dbFactory.newDocumentBuilder();
                 xmlDoc = docBuilder.newDocument();

                 Element root = xmlDoc.createElement(ViamoNetGlobalConstants.TAG_SYSTEM);
                 Element config=xmlDoc.createElement(ViamoNetGlobalConstants.TAG_CONFIGURATION);
                 root.appendChild(config);
                 xmlDoc.appendChild(root);
         
                 
                 for (int i=0; i<parametros.size();i++){
                         Parametro param=parametros.get(i);
                         config.appendChild(param.toXml(xmlDoc));
                 }
                 
                 //genero el string

                 StringWriter strWriter = null;
                 XMLSerializer xmlSerializer = null;
                 OutputFormat outFormat = null;

                 xmlSerializer = new XMLSerializer();
                 strWriter = new StringWriter();
                 outFormat = new OutputFormat();

                 outFormat.setEncoding("ISO-8859-1");
                 outFormat.setVersion("1.0");
                 outFormat.setIndenting(true);
                 outFormat.setStandalone(true);
                 outFormat.setIndent(4);

                 xmlSerializer.setOutputCharStream(strWriter);
                 xmlSerializer.setOutputFormat(outFormat);
                 xmlSerializer.serialize(xmlDoc);
                 strWriter.close();

                 xmlParametersStream=strWriter.toString();
                 return xmlParametersStream;

         } catch (ParserConfigurationException e) {
                 e.printStackTrace();
                 return null;
         } catch (IOException e) {
                 e.printStackTrace();
                 return null;
         }
 }
 
 
 private static boolean backupConfigFile(){
         
         try{
                 String xmlFileNameOrig=ViamoNetGlobalConstants.CONFIGURATION_FILE_FULL_NAME;
         
                 String xmlFileNameDest=ViamoNetGlobalConstants.CONFIGURATION_FILE_FULL_NAME+"_"+ViamoNetDateUtils.getTimeStamp();
                 
                 ViamoNetFileUtils.fileCopy(xmlFileNameOrig, xmlFileNameDest);
                 return true;
         }
         catch (Exception e)
         {
                 e.printStackTrace();
                 return false;
         }
 }
    
   
}
